package com.online.coding.exam.test;

import java.io.*;
import java.util.ArrayList;

/**
 * Created by Guanzhong Hu
 * Date :2019/12/19
 * Description :
 * Version :1.0
 */

public class CCompiler {
    private String answer;
    private ArrayList<Answer> cases;

    long path;

    public CCompiler(String answer, ArrayList<Answer> cases) {
        this.answer = answer;
        this.cases = cases;
    }

    public static void main(String[] args) {
        ArrayList<Answer> cases = new ArrayList<>();

        for (int i = 0;i<10;i++){
            Answer answer = new Answer();
            answer.setInput(i+"");
            answer.setOutput(i+"");
            answer.setGrade(10);
            cases.add(answer);
        }

        CCompiler compiler = new CCompiler("#include<stdio.h>\n" +
                "int main(){\n" +
                "\tint c=0;\n" +
                "\tscanf(\"%d\",&c);\n" +
                "\tprintf(\"%d\",c);\n" +
                "}", cases);
        try {
            System.out.println(compiler.getScore());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getScore() throws IOException {
        int score = 0;

        //先将答案存入文件
        if (save()) {
            //成功传入则编译
            if (compile()) {
                //编译成功则进行测试匹配
                for(Answer case_:cases){
                    if (run(case_.getInput()).equals(case_.getOutput()+"\n"))
                    {
                        score += case_.getGrade();
                    }

                }

            } else {
                return -2;
            }
        } else {
            return -1;
        }

        return score;
    }


    //保存为文件方法
    private boolean save() {
        boolean saved = false;
        path = System.currentTimeMillis();
        File file = new File("e:\\upload\\" + path);
        if (!file.exists()) file.mkdir();

        file = new File("./upload" + path + "/test.c");

        try {
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(answer);
            saved = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return saved;
    }

    //编译方法
    private boolean compile() throws IOException {
        boolean compiled = false;
        Process process = null;
        BufferedReader bufError = null;
        try {
            String compile = "cmd /c gcc e:\\upload\\" + path + "\\test.c -o e:\\upload\\" + path + "\\a.exe";
            process = Runtime.getRuntime().exec(compile);
            bufError = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GBK"));
            String tip = bufError.readLine();
            if (tip == null) {
                compiled = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) process.destroy();
            if (bufError != null) bufError.close();
        }
        return compiled;
    }

    //运行方法
    private String run(String input) throws IOException {
        StringBuilder result = new StringBuilder();
        BufferedReader buf = null;
        Process process = null;

        try {
            String run = "e:\\upload\\" + path + "\\a.exe";
            process = Runtime.getRuntime().exec(run);

            //进行输入
            process.getOutputStream().write(input.getBytes());
            process.getOutputStream().close();

            buf = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            String tip;
            while ((tip = buf.readLine()) != null) {
                result.append(tip).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) process.destroy();
            if (buf != null) buf.close();
        }
        return result.toString();
    }
}
